package cn.xpg.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xpg.entity.Orders;
import cn.xpg.service.OrdersService;
import cn.xpg.tools.PageSupport;

@Controller
public class OrdersController {

	@Resource(name = "ordersService")
	private OrdersService ordersService;

	// 分页查询订单
	@RequestMapping("/getOrdersList.do")
	public String getOrdersList(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String order_id = request.getParameter("order_id");
		String order_createtime = request.getParameter("order_createtime");
		String order_updatetime = request.getParameter("order_updatetime");
		int pageSize = 3;
		int currentPageNo = 1;

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int totalCount = 0;
		totalCount = ordersService.getOrdersCount(order_id, order_createtime, order_updatetime);
		System.out.println("查询订单信息！！！！！！！！！！————>getOrdersList");
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Orders> ordersList = null;
		ordersList = ordersService.getOrdersList(order_id, order_createtime, order_updatetime,
				(currentPageNo - 1) * pageSize, pageSize);
		for (Orders orders : ordersList) {
			System.out.println("!!!!!!!!!!!!!" + orders.getOrder_id());
		}

		request.setAttribute("ordersList", ordersList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("order_id", order_id);
		request.setAttribute("order_createtime", order_createtime);
		request.setAttribute("order_updatetime", order_updatetime);
		return "/jsp/repair/group_repair_list";
	}

	// 根据订单类型
	@RequestMapping("/getOrdersListByType.do")
	public String getOrdersListByType(HttpServletRequest request) {
		String order_id = request.getParameter("order_id");
		String order_createtime = request.getParameter("order_createtime");
		String order_updatetime = request.getParameter("order_updatetime");
		int order_type = Integer.valueOf(request.getParameter("order_type"));
		System.out.println("根据订单类型！！！！！！！！！！————>getOrdersListByType");
		List<Orders> ordersList = null;
		ordersList = ordersService.getOrdersListByType(order_type, order_id, order_createtime, order_updatetime);
		for (Orders orders : ordersList) {
			System.out.println("!!!!!!!!!!!!!" + orders.getOrder_id());
		}

		request.setAttribute("ordersList", ordersList);
		request.setAttribute("order_id", order_id);
		request.setAttribute("order_type", order_type);
		request.setAttribute("order_createtime", order_createtime);
		request.setAttribute("order_updatetime", order_updatetime);
		if (order_type == 1) {
			return "/jsp/repair/my_order_js";
		} else if (order_type == 2) {
			return "/jsp/repair/my_order_gz";
		} else if (order_type == 3) {
			return "/jsp/repair/my_order_fx";
		} else {
			return "/jsp/repair/my_order_tk";
		}
	}

	// 新增订单
	@RequestMapping("addOrders.do")
	public String addOrders(@Valid Orders orders, HttpServletRequest request) {
		System.out.println("新增订单信息！！！！！！！！！！————>addOrders");
		String bh = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		request.setAttribute("bh", bh);
		if (ordersService.addOrders(orders) > 0) {
			return "redirect:/getOrdersList.do";
		} else {
			return "/jsp/repair/group_repair_list";
		}
	}

	// 获取订单Id
	@RequestMapping("getOrdersId.do")
	public String getOrdersId(HttpServletRequest request) {
		System.out.println("获取订单Id！！！！！！！！！！————>getOrderId");
		int order_id = Integer.parseInt(request.getParameter("order_id"));
		Orders orders = ordersService.getOrdersId(order_id);
		request.setAttribute("orders", orders);
		return "/jsp/repair/order_update";
	}

	// 修改订单
	@RequestMapping("updateOrders.do")
	public String updateOrders(@Valid Orders orders, HttpServletRequest request) {
		System.out.println("修改订单信息！！！！！！！！！！————>updateOrders");
		if (ordersService.updateOrders(orders) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getOrdersList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("orders", orders);
			return "/jsp/repair/order_update";
		}
	}

	// 维修
	@RequestMapping("updateRepair.do")
	public String updateRepair(@Valid Orders orders, HttpServletRequest request) {
		System.out.println("修改订单信息！！！！！！！！！！————>updateRepair");
		if (ordersService.updateRepair(orders) > 0) {
			System.out.println("修改成功！！！！！！");
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("orders", orders);
		}
		return "redirect:/getOrdersList.do";
	}

	// 删除订单
	@RequestMapping("deleteOrders.do")
	public String deleteOrder(HttpServletRequest request) {
		System.out.println("删除订单信息！！！！！！！！！！————>deleteOrder");
		int order_id = Integer.valueOf(request.getParameter("order_id"));
		if (ordersService.deleteOrders(order_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getOrdersList.do";
	}

}
